package com.tuozixuan.onesql.ui;

import java.util.List;

import com.google.common.collect.Lists;

import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;

public class TabPaneContextMenu {
	
	private ContextMenu contextMenu;
	
	public TabPaneContextMenu(TabPane tabPane) {
		
        MenuItem closeMenuItem = this.buildCloseMenuItem(tabPane);
        MenuItem closeLeftMenuItem = this.buildCloseLeftMenuItem(tabPane);
        MenuItem closeRightMenuItem = this.buildCloseRightMenuItem(tabPane);
        MenuItem closeOtherMenuItem = this.buildCloseOtherMenuItem(tabPane);
        MenuItem closeAllMenuItem = this.buildCloseAllMenuItem(tabPane);
        
        contextMenu = new ContextMenu();
        contextMenu.getItems().addAll(closeMenuItem, closeLeftMenuItem, closeRightMenuItem, closeOtherMenuItem, new SeparatorMenuItem(), closeAllMenuItem);
	}

	public MenuItem buildCloseMenuItem(TabPane tabPane) {
		MenuItem closeMenuItem = new MenuItem("关闭");
		closeMenuItem.setOnAction(event -> {
            System.out.println("---------closeMenuItem clicked-----------" + event);
            Tab selectedTab = tabPane.getSelectionModel().getSelectedItem();
            tabPane.getTabs().remove(selectedTab);
            
        });
		return closeMenuItem;
	}
	
	public MenuItem buildCloseLeftMenuItem(TabPane tabPane) {
		MenuItem closeLeftMenuItem = new MenuItem("关闭左侧选项卡"); 
        closeLeftMenuItem.setOnAction(event -> {
            System.out.println("---------closeLeftMenuItem clicked-----------" + event);
            Tab selectedTab = tabPane.getSelectionModel().getSelectedItem();
            int selectedTabIndex = tabPane.getTabs().indexOf(selectedTab);
            List<Tab> needRemovedList = Lists.newArrayList();
            for (Tab tab : tabPane.getTabs()) {
                int localIndex = tabPane.getTabs().indexOf(tab);
                if (localIndex < selectedTabIndex) {
                    needRemovedList.add(tab);
                }
            }
            tabPane.getTabs().removeAll(needRemovedList);
        });
		return closeLeftMenuItem;
	}
	
	public MenuItem buildCloseRightMenuItem(TabPane tabPane) {
		MenuItem closeRightMenuItem = new MenuItem("关闭右侧选项卡"); 
		closeRightMenuItem.setOnAction(event -> {
            System.out.println("---------closeRightMenuItem clicked-----------" + event);
            Tab selectedTab = tabPane.getSelectionModel().getSelectedItem();
            int selectedTabIndex = tabPane.getTabs().indexOf(selectedTab);
            List<Tab> needRemovedList = Lists.newArrayList();
            for (Tab tab : tabPane.getTabs()) {
                int localIndex = tabPane.getTabs().indexOf(tab);
                if (localIndex > selectedTabIndex) {
                    needRemovedList.add(tab);
                }
            }
            tabPane.getTabs().removeAll(needRemovedList);
        });
		return closeRightMenuItem;
	}
	
	public MenuItem buildCloseOtherMenuItem(TabPane tabPane) {
		MenuItem closeOtherMenuItem = new MenuItem("关闭其他选项卡");
		closeOtherMenuItem.setOnAction(event -> {
            System.out.println("---------closeOtherMenuItem clicked-----------" + event);
            Tab selectedTab = tabPane.getSelectionModel().getSelectedItem();
            tabPane.getTabs().removeIf(tab -> !tab.equals(selectedTab));
        });
		return closeOtherMenuItem;
	}
	
	public MenuItem buildCloseAllMenuItem(TabPane tabPane) {
		MenuItem closeAllMenuItem = new MenuItem("全部关闭"); 
		closeAllMenuItem.setOnAction(event -> {
            System.out.println("---------closeAllMenuItem clicked-----------" + event);
            tabPane.getTabs().removeAll(tabPane.getTabs());
        });
		return closeAllMenuItem;
	}
	
	public ContextMenu getContextMenu() {
		return contextMenu;
	}
}
